package structure;


public class MyLinkedList {
    private static class LinkNode {
        int val;
        LinkNode next;

        LinkNode(int val) {
            this.val = val;
        }
    }

    // 头节点
    private LinkNode head;

    // 头插法
    public void addFirst(int data) {
        LinkNode node = new LinkNode(data);
        node.next = head;
        head = node;
    }

    // 尾插法
    public void addLast(int data) {
        LinkNode node = new LinkNode(data);
        if (head == null) {
            head = node;
            return;
        }

        LinkNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    // 插入指定位置
    public void addIndex(int data, int index) {
        if (index == 0) {
            addFirst(data);
        }
        if (index == size()) {
            addLast(data);
        }
        if (index < 0 || index > size()) {
            return;
        }
        LinkNode prev = findPrev(index);
        LinkNode node = new LinkNode(data);
        node.next = prev.next;
        prev.next = node;
    }

    // 找到index的前一个节点
    private LinkNode findPrev(int index) {
        LinkNode cur = head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    // 链表长度
    public int size() {
        LinkNode cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    // 查找值为 data 的元素是否在链表中
    public boolean contains(int data) {
        LinkNode cur = head;
        while (cur != null) {
            if (cur.val == data) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 删除第一次出现值为data的元素的节点
    public void remove(int data) {
        if (head == null) {
            return;
        }
        if (head.val == data) {
            head = head.next;
            return;
        }
        LinkNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == data) {
                // 找到了
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    public void removeAll(int data) {
        if (head.val == data) {
            head = head.next;
        }
        LinkNode prev = head;
        LinkNode cur = head.next;
        while (cur != null) {
            if (cur.val == data) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = prev.next;
            }
        }
    }

    public static void main(String[] args) {
        MyLinkedList linkedList = new MyLinkedList();
        linkedList.addLast(1);
        linkedList.addLast(2);
        linkedList.addLast(2);
        linkedList.addLast(4);

        linkedList.removeAll(2);
        System.out.println(1);
    }

    // 清空链表
    public void clear() {
        LinkNode cur = head;
        while (cur != null) {
            LinkNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

}
